<?php
namespace Home\Model;
use Think\Model;
use Common\Common\Classes\Message;

class QuestionService extends Model{
    private $que_sql;
    public function __construct(){
        $this->que_sql = D("Question");
    }
    //---------------------------------添加题目-----------------------------------------------
    public function addQuestion(){
        $where["qus_id"] = $_POST["qus_id"];
        $count = $this->que_sql->where($where)->count();
        $_POST["que_num"] = $count+1;     //题目num等于总题目数+1
        if($_POST["que_type"] == 4){     //如果是简答题的话,本系统是不帮助修改的!因此分数是0
            $_POST["que_score"] = 1;     //为了过自动验证,暂时改为1
            $_POST["que_answer"] = "1";
        }
        if($this->que_sql->create($_POST,1)){
            if($_POST["que_type"] == 4){
                $_POST["que_score"] = 0;     //恢复0
                $_POST["que_answer"] = "";
            }
            $result = $this->que_sql->add($_POST);
            if($result != ""){
                $data = new Message(1,"题目保存成功!",$result);
            }
            else{
                $data = new Message(0,"题目保存失败!","");
            }
        }
        else{
            $data = new Message(0,$this->que_sql->getError(),"");
        }
        return $data;
    }
    //---------------------------------题目上移---------------------------------
    public function moveUpQuestion(){
        $current = $this->que_sql->where($_POST)->field(array("que_num"))->find();
        if($current["que_num"] > 1){            //不是第一题才上移
            $where["qus_id"] = $_POST["qus_id"];
            $where["que_num"] = $current["que_num"] - 1;    //上一个
            $this->que_sql->startTrans();           //开启事务处理
            if($this->que_sql->where($where)->setInc("que_num",1) && $this->que_sql->where($_POST)->setDec("que_num",1)) {      //修改两个值!!
                $this->que_sql->commit();           //提交
                $data = new Message(1,"题目上移成功!","");
            }
            else{
                $this->que_sql->rollback();         //回滚
                $data = new Message(0,"题目上移失败!","");
            }
        }
        else{
            $data = new Message(0,"该题目已经是第一题!","");
        }
        return $data;
    }
    //---------------------------------题目下移-----------------------------------
    public function moveDownQuestion(){
        $current = $this->que_sql->where($_POST)->field(array("que_num"))->find();       //当前的que_num
        $count_where["qus_id"] = $_POST["qus_id"];
        $count = $this->que_sql->where($count_where)->count();      //总的题目数目
        if($current["que_num"]<$count){     //可以下移的情况
            $where["qus_id"] = $_POST["qus_id"];
            $where["que_num"] = $current["que_num"] + 1;    //上一个
            $this->que_sql->startTrans();           //开启事务处理
            if($this->que_sql->where($where)->setDec("que_num",1) && $this->que_sql->where($_POST)->setInc("que_num",1)){
                $this->que_sql->commit();           //提交
                $data = new Message(1,"题目下移成功!","");
            }
            else{
                $this->que_sql->rollback();     //回滚
                $data = new Message(0,"题目下移失败!","");
            }
        }
        else{
            $data = new Message(0,"该题目已经是最后一题!","");
        }
        return $data;
    }
    //---------------------------------删除题目(单个)-----------------------------------
    //这个删除可能需要修改其他题目的编号que_num
    public function deleteQuestion(){
        $current = $this->que_sql->where($_POST)->field(array("que_num"))->find();       //要删除的题目的que_num
        $count_where["qus_id"] = $_POST["qus_id"];
        $count = $this->que_sql->where($count_where)->count();      //总的题目数目
        if($current["que_num"] != $count){      //不是最后一题，要修改其他题目的num
            $where["qus_id"] = $_POST["qus_id"];
            $where["que_num"] = array("gt",$current["que_num"]);    //题目编号大于$current["que_num"]都要修改
            $this->que_sql->startTrans();           //开始事务
            if($this->que_sql->where($_POST)->delete() && $this->que_sql->where($where)->setDec("que_num",1) == $count-$current["que_num"]){
                $this->que_sql->commit();       //提交
                $data = new Message(1,"题目删除成功!","");
            }
            else {
                $this->que_sql->rollback();     //回滚
                $data = new Message(0,"题目删除失败!","");
            }
        }
        else{
            if($this->que_sql->where($_POST)->delete()){
                $data = new Message(1,"题目删除成功!","");
            }
            else{
                $data = new Message(0,"题目删除失败!","");
            }
        }
        return $data;
    }
    //---------------------------------联级删除题目-----------------------------------------------
    public function RelationDeleteQuestion($que_id){     //参数是一个题目的id,可以是一个数组,也可以是单个
        $where["que_id"] = array("in",$que_id);
        $successNum = $this->que_sql->relation("delete_question")->where($where)->delete();
        if($successNum>0){
            $data = new Message(1,"题目删除成功!","");
        }
        else{
            $data = new Message(0,"题目删除失败!","");
        }
        return $data;
    }
    //---------------------------------显示某张试卷的所有题目--------------------------
    public function getQuizQuestion($field){      //试卷id
        return $this->que_sql->where($_POST)->field($field)->order("que_num")->select();
    }
    //---------------------------------处理所有题目的格式(捎去null)--------------------------
    public function unsetQusetion($questions){
        for($i = 0;$i<count($questions);$i++){
            for($j = 0;$j<8;$j++){
                if($questions[$i]["que_item".($j+1)] != null){          //清除null的选项
                    $questions[$i]["que_item"][$j] = array("content" => $questions[$i]["que_item".($j+1)]);
                }
                unset($questions[$i]["que_item".($j+1)]);
            }
        }
        return $questions;
    }
    //---------------------------------修改题目-----------------------------------------------
    public function updateQuestion(){
        if($this->que_sql->create($_POST,2)){
            $where["que_id"] = $_POST["que_id"];        //条件
            $update = $_POST;           //修改内容
            unset($update["que_id"]);   //修改内容去除不需要的信息
            $result = $this->que_sql->where($where)->save($update);
            if($result>0){
                $data = new Message(1,"题目修改成功!","");
            }
            else{
                $data = new Message(0,"题目修改失败!","");
            }
        }
        else{
            $data = new Message(0,$this->que_sql->getError(),"");
        }
        return $data;
    }
    //---------------------------------计算某张试卷的总分-----------------------------------------------
    public function countScore(){
        return $this->que_sql->where($_POST)->sum("que_score");
    }
    //---------------------------------分数分组处理-----------------------------------------------
    public function grouping($total_score){
        if($total_score+1<=6){              //总分+1小于等于6的分法
            $divide = $total_score + 1;
            for($i = 0;$i<$divide;$i++){
                $group[$i] = array($i,$i);
            }
        }
        else {
            $divide = 6;
            $chaju = ($total_score + 1) % 6 / floor(($total_score + 1) / $divide);
            if ($chaju == 0) {
                $divide = 6;
            } else {
                for ($i = 7; $i < 10; $i++) {           //最多分10组  最少分1组
                    $temp_chaju = (($total_score + 1) % $i) / (floor(($total_score + 1) / $i));
                    if ($temp_chaju == 1 || $temp_chaju == 0) {
                        $divide = $i;
                        break;          //特殊情况退出循环
                    }
                    else if (abs(1-$temp_chaju) < abs(1-$chaju)) {
                        $chaju = $temp_chaju;
                        $divide = $i;
                    }
                }
            }
            $start = 0;     //组织分组
            $end = floor(($total_score + 1) / $divide) - 1;
            for ($i = 0; $i < $divide; $i++) {
                $group[$i] = array($start,$end);
                $start = $end + 1;
                if ($i < $divide - 1) {            //正常处理
                    $end = $start + floor(($total_score + 1) / $divide) - 1;
                } else {                //增组处理
                    $end = $start + ($total_score + 1) % $divide - 1;
                }
            }
            if (($total_score + 1) % $divide != 0) {       //增组处理
                $group[$i] = array($start,$end);
            }
        }
        return $group;
    }
    //---------------------------------检测权限-----------------------------------------------
    public function checkPower(){
        $where["teacher_tea_id"] = $_SESSION["id"];
        $where["que_id"] = $_POST["que_id"];
        $result = D("CheckQuestionView")->where($where)->find();
        if($result != ""){
            $data = new Message(1,"你拥有权限对该题目进行操作!","");
        }
        else{
            $data = new Message(0,"你没有权限对该题目进行操作!","");
        }
        return $data;
    }
    public function __destruct(){}
}